package user.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import user.User;
import user.UserCheck;
import user.dao.UserDAOInterface;

public class UserDAOJPAImpl implements UserDAOInterface {
	
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("CAS-Security");
	EntityManager em = emf.createEntityManager();
	EntityTransaction tx = em.getTransaction();
	
	@Override
	public void addUser(User user) {
		try{
			tx.begin();
			em.persist(user);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public User findUserByID(String userid) {
		User user = null;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT u FROM User u WHERE u.userId=:id");
			query.setParameter("id", userid);
			user = (User) query.getSingleResult();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			return user;
		}
		return user;
	}

	@Override
	public List<User> getUsers() {
		List<User> userList = null;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT u FROM User u");
			userList = query.getResultList();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			return userList;
		}
		return userList;
	}

	@Override
	public void modifyUser(User user) {
		try{
			tx.begin();
			em.merge(user);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}

	@Override
	public void removeUser(String userid) {
		try{
			tx.begin();
			User user = this.findUserByID(userid);
			user = em.merge(user);
			em.remove(user);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
	@Override
	public void addUserCheck(UserCheck uc) {
		try{
			tx.begin();
			em.persist(uc);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
	@Override
	public List<UserCheck> findUserCheckByUserID(String userid) {
		try{
			tx.begin();
			Query query = em.createQuery("");
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
		return null;
	}
	@Override
	public UserCheck findUserCheckByUserName(String username) {
		UserCheck uc = null;
		try{
			tx.begin();
			Query query = em.createQuery("SELECT uc FROM UserCheck uc WHERE uc.username =:name");
			query.setParameter("name", username);
			uc = (UserCheck) query.getSingleResult();
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			return uc;
		}
		return uc;
	}
	@Override
	public void modifyUserCheck(UserCheck uc) {
		try{
			tx.begin();
			em.merge(uc);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
	@Override
	public void removeUserCheck(String username) {
		try{
			tx.begin();
			UserCheck uc = this.findUserCheckByUserName(username);
			UserCheck mergeduc = em.merge(uc);
			em.remove(mergeduc);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
		}
	}
}
